package pers.xiaojun.boot.common.pojo;

import cn.hutool.captcha.AbstractCaptcha;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import cn.hutool.captcha.generator.CodeGenerator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import pers.xiaojun.boot.common.config.properties.CaptchaProperties;

/**
 * 图片验证码
 *
 * @author xiaojun
 * @since 2025-10-06
 */

@Slf4j
@RequiredArgsConstructor
public class ImageCaptcha {

    private final CaptchaProperties captchaProperties;
    private final CodeGenerator codeGenerator;

    public AbstractCaptcha createDefaultCaptcha() {
        // 干扰线
        if (captchaProperties.getDisturbType() == CaptchaProperties.DisturbType.LINE) {
            return createLineCaptcha();
        }
        // 圆圈干扰
        if (captchaProperties.getDisturbType() == CaptchaProperties.DisturbType.CIRCLE) {
            return createCircleCaptcha();
        }
        // 扭曲干扰
        if (captchaProperties.getDisturbType() == CaptchaProperties.DisturbType.SHEAR) {
            return createShearCaptcha();
        }
        log.error("验证码创建失败");
        return null;
    }

    public AbstractCaptcha createLineCaptcha() {
        return CaptchaUtil.createLineCaptcha(
                captchaProperties.getWidth(),
                captchaProperties.getHeight(),
                codeGenerator,
                captchaProperties.getDisturbCount()
        );
    }

    public AbstractCaptcha createCircleCaptcha() {
        return CaptchaUtil.createCircleCaptcha(
                captchaProperties.getWidth(),
                captchaProperties.getHeight(),
                codeGenerator,
                captchaProperties.getDisturbCount()
        );
    }

    public AbstractCaptcha createShearCaptcha() {
        return CaptchaUtil.createShearCaptcha(
                captchaProperties.getWidth(),
                captchaProperties.getHeight(),
                codeGenerator,
                captchaProperties.getDisturbCount()
        );

    }

}
